/*
 *    Webical - http://www.webical.org
 *    Copyright (C) 2007 Func. Internet Integration
 *
 *    This file is part of Webical.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.webical.web.component.settings;

import org.apache.wicket.spring.injection.annot.SpringBean;
import org.webical.User;
import org.webical.UserSettings;
import org.webical.manager.SettingsManager;
import org.webical.manager.UserManager;
import org.webical.manager.WebicalException;
import org.webical.web.action.IAction;
import org.webical.web.action.ShowCalendarAction;
import org.webical.web.app.WebicalSession;
import org.webical.web.app.WebicalWebAplicationException;
import org.webical.web.component.AbstractBasePanel;
import org.webical.web.component.behavior.FormComponentValidationStyleBehavior;

/**
 * Panel that holds the form for editing {@link User} information and application settings
 *
 * @author Mattijs Hoitink
 *
 */
public abstract class UserSettingsPanel extends AbstractBasePanel {
	private static final long serialVersionUID = 1L;

	// MARKUP ID's
	private static final String USER_SETTINGS_FORM_MARKUP_ID = "userSettingsForm";
	
	@SpringBean(name="userManager")
	private UserManager userManager;

	@SpringBean(name="settingsManager")
	private SettingsManager settingsManager;

	private UserSettingsForm userSettingsForm;

	private User user;
	private UserSettings userSettings;

	/**
	 * Default constructor
	 * @param markupId The ID to use in markup
	 */
	public UserSettingsPanel(String markupId) {
		super(markupId, UserSettingsPanel.class);
	}

	/* (non-Javadoc)
	 * @see org.webical.web.component.IAccessibilitySwitchingComponent#setupCommonComponents()
	 */
	public void setupCommonComponents() {

		this.user = WebicalSession.getWebicalSession().getUser();
		this.userSettings = WebicalSession.getWebicalSession().getUserSettings();
		
		userSettingsForm = new UserSettingsForm(USER_SETTINGS_FORM_MARKUP_ID, user, userSettings) {
			private static final long serialVersionUID = 1L;

			@Override
			public void storeUserSettings(User user, UserSettings userSettings) {
				// Store the user information
				try {
					userManager.storeUser(user);
				} catch (WebicalException ue) {
					throw new WebicalWebAplicationException(ue);
				}

				// Store the settings information
				try {
					settingsManager.storeUserSettings(userSettings);
				} catch (WebicalException se) {
					throw new WebicalWebAplicationException(se);
				}
				
				UserSettingsPanel.this.onAction(new ShowCalendarAction());
			}

		};
		userSettingsForm.add(new FormComponentValidationStyleBehavior());

		addOrReplace(userSettingsForm);	}

	/* (non-Javadoc)
	 * @see org.webical.web.component.IAccessibilitySwitchingComponent#setupAccessibleComponents()
	 */
	public void setupAccessibleComponents() {
		// NOTHING TO DO
	}

	/* (non-Javadoc)
	 * @see org.webical.web.component.IAccessibilitySwitchingComponent#setupNonAccessibleComponents()
	 */
	public void setupNonAccessibleComponents() {
		// NOTHING TO DO
	}

	/**
	 * Handle actions generated by this panel
	 * @param action The action to handle
	 */
	abstract void onAction(IAction action);

	/**
	 * Used by Spring
	 * @param userManager a UserManager implementation
	 */
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	/**
	 * Used by Spring
	 * @param settingsManager a SettingsManager implementation
	 */
	public void setSettingsManager(SettingsManager settingsManager) {
		this.settingsManager = settingsManager;
	}
}
